package com.zyzy.leftfourdead.adapter.groupBuyAdapter;

import android.content.Context;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zyzy.leftfourdead.R;
import com.zyzy.leftfourdead.bean.PreferenceBean;
import com.zyzy.leftfourdead.bean.PrizeBean;

import java.util.List;

/**
 * Created by loverforever on 2016/8/24.
 */
public class PrizeViewPagerFragmentAdapter extends PagerAdapter {
    private Context context;
    private List<PrizeBean.BannersBean> list=null;
    private LayoutInflater inflater=null;
    public PrizeViewPagerFragmentAdapter(Context context,List<PrizeBean.BannersBean>list){
        this.context=context;
        this.list=list;
        inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view=inflater.inflate(R.layout.item_fragment_groupbuy_viewpager,container,false);
        //初始化图片控件
        SimpleDraweeView simpleDraweeView_groupBuy_viewPager_item= (SimpleDraweeView) view.findViewById(R.id.simpleDraweeView_groupBuy_viewPager_item);
        //给控件设置获取到的图片
        simpleDraweeView_groupBuy_viewPager_item.setImageURI(Uri.parse(list.get(position).getPath()));
        container.addView(view);
        return view;
    }

    //刷新数据
    public void reloadGridView(List<PrizeBean.BannersBean> _list, boolean isClear) {
        if (isClear) {
            list.clear();
        }
        list.addAll(_list);
        //刷新
        notifyDataSetChanged();
    }

    //Gson解析
    public PrizeBean parseJsonToList(String json) {
        //创建gson对象
        Gson gson = new Gson();
        //第一个参数 json字符串
        //第二个参数 HotBean对象
        //格式就是这么写的 有问题问gson工程师去
        return gson.fromJson(json, new TypeToken<PrizeBean>() {
        }.getType());
    }
}
